package com.lonelyzhe.springboot.controller;


import com.lonelyzhe.springboot.bean.User;
import com.lonelyzhe.springboot.bean.vo.Login;
import com.lonelyzhe.springboot.service.impl.UserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("user")
@ResponseBody
public class UserController extends BaseController {

    @Autowired
    UserService userService;

    @RequestMapping("/login")
    public AjaxResult allList(@RequestBody Login login_info, HttpServletResponse response) {
        try {
            User user = userService.checkLogin(login_info);
            if(user == null){
                return AjaxResult.error("用户名或密码不正确");
            } else {
//                String val;
//                val = user.getId()+"-"+user.getUserName()+"-"+user.getNickName()+"-"+user.getMobilephone()+"-"+user.getAddress()+"-"+user.getEmail();
//                Cookie cookie = new Cookie("user_info",val);
//                cookie.setPath("/");
//                response.addCookie(cookie);
//                response.setHeader("Access-Control-Allow-Credentials", "true");
//                response.setCharacterEncoding("UTF-8");
                return AjaxResult.success(user);
            }
        } catch (Exception e) {
            return AjaxResult.error("发生错误");
        }
    }

    @RequestMapping("/register")
    public AjaxResult register(@RequestBody User user) {
        try {
            userService.register(user);
            return AjaxResult.success("注册成功");
        } catch (Exception e) {
            return AjaxResult.error("发生错误");
        }
    }
}
